<template>
  <div class="app">
    <!-- 公告内容区域 -->
    <main class="notice-container">
      <div class="container">
        <div class="section-title">
          <h2>公告中心</h2>
          <p>了解平台最新动态和服务变更</p>
        </div>

        <!-- 公告列表 -->
        <div class="notice-list">
          <div v-if="loading" class="loading">
            <el-icon class="is-loading"><Loading /></el-icon>
            加载中...
          </div>

          <div v-else-if="notices.length === 0" class="empty-notice">
            暂无公告
          </div>

          <div v-else>
            <div class="notice-card"
                 v-for="notice in notices"
                 :key="notice.id"
                 @click="showNoticeDetail(notice)">
              <div class="notice-header">
                <h3>{{ notice.title }}</h3>
                <span class="notice-date">{{ formatDate(notice.publishTime) }}</span>
                <span class="notice-views">
                  <el-icon><View /></el-icon>
                  <span class="view-count">{{ notice.viewCount || 0 }}</span>次浏览
                </span>
              </div>
              <div class="notice-content">
                {{ truncateContent(notice.content) }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </main>

    <!-- 公告详情对话框 -->
    <el-dialog
        v-model="dialogVisible"
        :title="currentNotice.title"
        width="60%"
        :before-close="handleClose"
        custom-class="notice-dialog"
    >
      <div class="notice-detail-meta">
        <span class="publish-time">发布时间: {{ formatDateTime(currentNotice.publishTime) }}</span>
        <span class="view-count">浏览数: {{ currentNotice.viewCount || 0 }}</span>
      </div>
      <div class="notice-detail-content">
        {{ currentNotice.content }}
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>

  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { ArrowDown, Loading } from '@element-plus/icons-vue'

const router = useRouter()
const notices = ref([
  {
    id: '',
    title: '',
    content: '',
    viewCount: '',
    publishTime: ''
  }
])

const loading = ref(true)

// 下拉菜单相关逻辑
const activeDropdown = ref(null)
const hoverTimeout = ref(null)

const dropdownOptions = {
  'escort': [
    { name: '陪诊师列表', path: '/escort/list' },
    { name: '陪诊师认证', path: '/escort/certification' }
  ],
  'about': [
    { name: '项目简介', path: '/about' },
    { name: '联系我们', path: '/contact' }
  ]
}

// 公告详情对话框相关
const dialogVisible = ref(false)
const currentNotice = ref({
  id: '',
  title: '',
  content: '',
  viewCount: 0,
  publishTime: ''
})

// 获取公告列表
const fetchNotices = async () => {
  try {
    loading.value = true
    const response = await axios.get(BASE_URL+'/v1/notice/notices')
    console.log(response.data);
    if (response.data.state === 2000) {
      notices.value = response.data.data
    } else {
      ElMessage.error(response.data.message)
    }
  } catch (error) {
    console.error('获取公告失败:', error)
    ElMessage.error('获取公告失败，请稍后重试')
  } finally {
    loading.value = false
  }
}

// 显示公告详情
const showNoticeDetail = async (notice) => {
  try {
    // 先本地更新，提升用户体验
    notice.viewCount = Number(notice.viewCount || 0) + 1
    currentNotice.value = { ...notice }
    dialogVisible.value = true

    // 发送请求到后端更新浏览量
    await axios.get(`${BASE_URL}/v1/notice/addViewCount`, {
      params: {
        id: notice.id
      }
    })
  } catch (error) {
    // 如果请求失败，回滚本地计数
    notice.viewCount = Math.max(0, (notice.viewCount || 0) - 1)
    console.error('更新浏览数失败:', error)
  }
}


// 格式化日期
const formatDate = (timestamp) => {
  if (!timestamp) return ''
  const date = new Date(timestamp)
  return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`
}


// 格式化日期时间 (YYYY-MM-DD HH:mm:ss)
const formatDateTime = (timestamp) => {
  if (!timestamp) return ''
  const date = new Date(timestamp)
  return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`
}

// 截断内容，用于列表显示
const truncateContent = (content) => {
  if (!content) return ''
  return content.length > 100 ? content.substring(0, 100) + '...' : content
}

const handleClose = (done) => {
  done()
}


// 组件挂载时获取公告
onMounted(() => {
  fetchNotices()
})
</script>

<style scoped>
/* 保持原有的全局样式 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Microsoft YaHei', Arial, sans-serif;
}

body {
  background-color: #f5f7fa;
  color: #333;
  line-height: 1.6;
}

a {
  text-decoration: none;
  color: inherit;
}

ul {
  list-style: none;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 15px;
}

.btn {
  display: inline-block;
  padding: 10px 20px;
  background-color: #4a90e2;
  color: white;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
  border: none;
  font-size: 16px;
}

.btn:hover {
  background-color: #3a7bc8;
}

.btn-secondary {
  background-color: #6c757d;
}

.btn-secondary:hover {
  background-color: #5a6268;
}

/* 导航栏样式 */
header {
  background-color: white;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 0;
}

.logo {
  font-size: 24px;
  font-weight: bold;
  color: #4a90e2;
}

.logo span {
  color: #333;
}



.nav-links a {
  font-weight: 500;
  transition: color 0.3s;
}

.nav-links a:hover {
  color: #4a90e2;
}

.nav-links a.active {
  color: #4a90e2;
  font-weight: bold;
}


/* 公告容器 */
.notice-container {
  padding: 60px 0;
  min-height: calc(100vh - 200px); /* 确保内容区域足够高 */
}

.section-title {
  text-align: center;
  margin-bottom: 50px;
}

.section-title h2 {
  font-size: 32px;
  color: #2c3e50;
  margin-bottom: 15px;
}

.section-title p {
  color: #777;
  max-width: 700px;
  margin: 0 auto;
}

/* 公告列表样式 */
.notice-list {
  max-width: 900px;
  margin: 0 auto;
}

.notice-card {
  background-color: white;
  border-radius: 8px;
  padding: 25px;
  margin-bottom: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  transition: transform 0.3s, box-shadow 0.3s;
}

.notice-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.notice-header {
  display: flex;
  align-items: center;  /* 垂直居中 */
  justify-content: space-between;  /* 标题和日期分别靠两端 */
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
  position: relative;  /* 为绝对定位的日期做准备 */
}

.notice-header h3 {
  flex: 1;  /* 标题占据剩余空间 */
  margin-right: 20px;  /* 与日期保持间距 */
  font-size: 18px;
  color: #333;
}

.notice-date {
  position: absolute;  /* 绝对定位 */
  left: 50%;  /* 居中定位 */
  transform: translateX(-50%);  /* 精确居中 */
  font-size: 14px;
  color: #888;
  background-color: #f8f9fa;
  padding: 2px 10px;
  border-radius: 12px;
  white-space: nowrap;  /* 防止日期换行 */
}

.notice-views {
  display: inline-flex;
  align-items: center;
  font-size: 14px;
  color: #666;
  background-color: #f5f7fa;
  padding: 4px 8px;
  border-radius: 12px;
  margin-left: 10px;
}

.notice-views .el-icon {
  margin-right: 4px;
  color: #888;
}

.view-count {
  font-weight: bold;
  color: #4a90e2;
  margin: 0 2px;
}

/* 响应式设计 - 小屏幕时调整布局 */
@media (max-width: 768px) {
  .notice-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .notice-date {
    position: static;
    transform: none;
    margin-top: 8px;
    align-self: flex-end;  /* 在小屏幕时日期靠右 */
  }
}

/* 悬停效果 */
.notice-views:hover {
  background-color: #e8f0fe;
}

.notice-views:hover .view-count {
  color: #3a7bc8;
}

.notice-content {
  color: #555;
  line-height: 1.7;
  white-space: pre-line;
}

/* 加载状态和空状态 */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: #666;
}

.loading .el-icon {
  margin-right: 10px;
  animation: rotating 2s linear infinite;
}

.empty-notice {
  text-align: center;
  padding: 50px;
  color: #888;
  font-size: 16px;
}

@keyframes rotating {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}



.dropdown-menu li {
  padding: 8px 20px;
  transition: background-color 0.3s;
}

.dropdown-menu li:hover {
  background-color: #f5f7fa;
}

.dropdown-menu a {
  display: block;
  color: #333;
  white-space: nowrap;
}

.dropdown-menu a:hover {
  color: #4a90e2;
}

.nav-links > li {
  position: relative;
}

/* 公告卡片样式调整 */
.notice-card {
  cursor: pointer;
  transition: all 0.3s;
}

.notice-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  background-color: #f8fafc;
}

.notice-content {
  color: #555;
  line-height: 1.7;
  /* 限制行数并显示省略号 */
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 公告详情对话框样式 */
.notice-detail-meta {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  color: #666;
  font-size: 14px;
  padding: 0 10px;
}

.notice-detail-content {
  padding: 10px;
  line-height: 1.8;
  white-space: pre-line;
}

/* 对话框自定义样式 */
:deep(.notice-dialog) {
  border-radius: 8px;
}

:deep(.notice-dialog .el-dialog__header) {
  border-bottom: 1px solid #eee;
  padding-bottom: 15px;
}

:deep(.notice-dialog .el-dialog__title) {
  font-size: 20px;
  color: #333;
}

:deep(.notice-dialog .el-dialog__body) {
  padding: 20px;
}

:deep(.notice-dialog .el-dialog__footer) {
  border-top: 1px solid #eee;
  padding-top: 15px;
  text-align: center;
}
/* 页脚样式 */
footer {
  background-color: #2c3e50;
  color: #fff;
  padding: 60px 0 20px;
}

.footer-content {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 40px;
  margin-bottom: 40px;
}

.footer-section h3 {
  font-size: 18px;
  margin-bottom: 20px;
  color: #fff;
}

.footer-section p {
  margin-bottom: 15px;
  color: #bbb;
}

.footer-section ul li {
  margin-bottom: 10px;
}

.footer-section a {
  color: #bbb;
  transition: color 0.3s;
}

.footer-section a:hover {
  color: #4a90e2;
}

.copyright {
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid #3d5166;
  color: #bbb;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .nav-links {
    display: none;
  }

  .notice-container {
    padding: 40px 0;
  }

  .notice-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .notice-date {
    margin-top: 5px;
  }

}
</style>